package com.ideabove.heart;

import com.ideabove.heart.R;
import com.ideabove.heart.db.DBHelper;
import com.ideabove.heart.manager.AppManager;
import com.ideabove.heart.manager.ScheduleManager;
import com.ideabove.heart.manager.ScheduleManager.Schedule;
import com.nineoldandroids.animation.ObjectAnimator;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Intent;

public class IntroActivity extends MyActivity {

	Intent mIntent;
	Thread ttt;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.intro);

		mIntent = getIntent();
		final Bundle bundle = mIntent.getExtras();    

		if(bundle == null){      			
			ScheduleManager scheMgr = new ScheduleManager(IntroActivity.this);	
			if(scheMgr.isMissedCall()){ // 부재중인가요?
				showFail(); // 부재중 화면
			}else{
				initFirst();
				showMain(); // 메인화면
			}

		}else{ //알람이 왔을경우..
			moveAlarmActivity();
		}		
	}

	public void showFail(){
		Intent iAlarm = new Intent(this, FailActivity.class);
		iAlarm.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(iAlarm);
		finish();
	}

	public void showMain(){

		ScheduleManager sche = new ScheduleManager(this);
		
		// 처음 들어올때
		if(sche.getLastSchedule().getId() > 0){
			ScheduleManager scheMgr = new ScheduleManager(IntroActivity.this);											
			Schedule schedule = scheMgr.whereAmI(new DBHelper(IntroActivity.this).open());
			schedule.doIt();			
		}else{
			// 담에 들어올때
			if(ttt == null){
				ttt = new Thread(){
					public void run(){
						try {
							Thread.sleep(2000);
							if(!ttt.isInterrupted()){
								runOnUiThread(new Runnable(){
									@Override
									public void run() {
										// 해당 작업을 처리함
										ImageView img = (ImageView)findViewById(R.id.intro_view_img);
										img.setImageResource(R.drawable.intro);
	
										ObjectAnimator animation1 = ObjectAnimator.ofFloat(img,"alpha", 0f, 1f);
										animation1.setDuration(1000);
										animation1.start();
	
										img.setOnClickListener(new OnClickListener() {
											@Override
											public void onClick(View v) {
												// TODO Auto-generated method stub        	
												ScheduleManager scheMgr = new ScheduleManager(IntroActivity.this);											
												Schedule schedule = scheMgr.whereAmI(new DBHelper(IntroActivity.this).open());
												schedule.doIt();
												ttt = null;
											}
										});
									}
								});					
							}
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				};
				ttt.start();
			}
		}
	}

	public void initFirst(){
		// sound init
		AppManager.loadSound(this);

		// bg sound set
		float volume = ScheduleManager.getCurrentBgVolume(this);    	
		AppManager.setBgVolume(volume);
		AppManager.start();
	}

	public void moveAlarmActivity(){

		Intent iAlarm = new Intent(this, MorningCallActivity.class);
		iAlarm.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);	
		startActivity(iAlarm);        
		finish();
	}

	@Override
	protected void onNewIntent(Intent intent) {

		super.onNewIntent(intent);

		if(intent != null && intent.getExtras() != null && "alarm_start".equals( intent.getExtras().get("q")) ){			
			moveAlarmActivity();
		}
	}


	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AppManager.pause(this);
	}

	@Override
	protected void onDestroy(){
		super.onDestroy();
	}
}
